package com.nai365.nart.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nai365.nart.common.core.constant.SecurityConstants;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.operation.dto.DrawLotsSinUpDto;
import com.nai365.nart.operation.entity.DrawLotsSignUp;
import com.nai365.nart.operation.entity.OptDrawLotsLog;
import com.nai365.nart.operation.mapper.OptDrawLotsLogMapper;
import com.nai365.nart.operation.service.DrawLotsSignUpService;
import com.nai365.nart.operation.service.OptDrawLotsLogService;
import com.nai365.nart.operation.vo.DrawLotsVo;
import com.nai365.nart.order.api.feign.OrderFeignService;
import com.nai365.nart.order.to.DrawOrderTo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
public class OptDrawLotsLogServiceImpl extends ServiceImpl<OptDrawLotsLogMapper, OptDrawLotsLog> implements OptDrawLotsLogService {

	private final  DrawLotsSignUpService drawLotsSevice;

	private final OrderFeignService orderFeignService;

	@Override
	public List<OptDrawLotsLog> drawLots(DrawLotsVo drawLotsVo) {
		/**
		 * 1. 根据商品查询本期报名用户
		 * 2.根据报名用户 ，按照份数生成订单，及抽签记录
		 */
		List<DrawLotsSignUp> userSignUpBySerialNumber = drawLotsSevice.findUserSignUpByGoodsId(drawLotsVo.getGoodsId());
		Collections.shuffle(userSignUpBySerialNumber);
		List<OptDrawLotsLog> lotsLogs = userSignUpBySerialNumber.parallelStream()
				.limit(drawLotsVo.getTotalNum())
				.map(x -> BeanUtil.copyProperties(x, OptDrawLotsLog.class)).collect(Collectors.toList());

		//保存记录
		this.saveBatch(lotsLogs);
		//TODO
		Map<Long, Long> m = lotsLogs.parallelStream().collect(Collectors.toConcurrentMap(OptDrawLotsLog::getUserId, OptDrawLotsLog::getGoodsId));
		//调用远程创单接口
		R<List<DrawOrderTo>> listR = orderFeignService.drawCreateOrder(m, SecurityConstants.FROM_IN);

		Map<Long,String> orders=listR.getData().parallelStream().collect(Collectors.toConcurrentMap(DrawOrderTo::getUserId,DrawOrderTo::getOrderSn));

//		//TODO
		List<OptDrawLotsLog> optDrawLotsLogs = lotsLogs.stream().map(x -> {
			x.setOrderSn(orders.get(x.getUserId()));
			return x;
		}).collect(Collectors.toList());
		this.saveOrUpdateBatch(optDrawLotsLogs);
		return lotsLogs;
	}

	@Override
	public DrawLotsSinUpDto findUserdrawLotsByUserIdAndGoodsId(Long userId, Long goodsId) {
		LambdaQueryWrapper<OptDrawLotsLog> optDrawLotsLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
		optDrawLotsLogLambdaQueryWrapper.eq(OptDrawLotsLog::getUserId,userId);
		optDrawLotsLogLambdaQueryWrapper.eq(OptDrawLotsLog::getGoodsId,goodsId);
		OptDrawLotsLog drawLotsLog = this.getOne(optDrawLotsLogLambdaQueryWrapper);
		//============
		DrawLotsSignUp userSignUp = drawLotsSevice.findUserSignUp(userId, goodsId);

		//DrawLotsSinUpDto drawLotsSinUpDto = new DrawLotsSinUpDto()
		DrawLotsSinUpDto drawLotsSinUpDto = new DrawLotsSinUpDto(new DrawLotsSignUp(), new OptDrawLotsLog());

		if(!ObjectUtils.isEmpty(drawLotsLog)){
			drawLotsSinUpDto.setOptDrawLotsLog(drawLotsLog);
		}
		if(!ObjectUtils.isEmpty(userSignUp)){
			drawLotsSinUpDto.setSignUp(userSignUp);
		}

		return drawLotsSinUpDto;
	}
}
